﻿//-----------------------------------------------------------------------
// <copyright file="GoatField.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace GoatGen.Object
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Base class for a field inside of a generated Goat class.
    /// </summary>
    public abstract class GoatField
    {
        #region Constructor

        /// <summary>
        /// Constructor for a field.
        /// </summary>
        /// <param name="name">Name of the field.</param>
        /// <param name="properties">Properties for the field. Min, max, etc. Each field type defines
        /// the properties that it supports.</param>
        public GoatField(string name, GoatFieldProperties properties)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "Field name must be non-null.");
            }

            if (properties == null)
            {
                throw new ArgumentNullException("properties", "Properties must be non-null.");
            }

            this.Name = name;
            this.Properties = properties;
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets the name of the field.
        /// </summary>
        public string Name
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the C# type of the field.
        /// </summary>
        public abstract Type Type
        {
            get;
        }

        #endregion

        #region Properties - Protected

        /// <summary>
        /// Gets the properties for the Goat field.
        /// </summary>
        protected GoatFieldProperties Properties
        {
            get;
            private set;
        }

        #endregion

        #region Methods - Protected

        /// <summary>
        /// Parse a float array from the passed property name.
        /// </summary>
        /// <param name="name">Property name to parse a float array from.</param>
        /// <param name="expectedCount">Expected number of items in the array.</param>
        /// <param name="min">Min value.</param>
        /// <param name="max">Max value.</param>
        /// <returns>Returns the float array if we could parse it or null if we could not.</returns>
        protected float[] ParseFloatArray(string name, int expectedCount, float min = float.MinValue, float max = float.MaxValue)
        {
            string source = this.Properties.GetString(name, null);
            if (string.IsNullOrEmpty(source))
            {
                return null;
            }

            string[] parts = source.Split(' ');
            if (parts == null)
            {
                return null;
            }

            if (parts.Length != expectedCount)
            {
                return null;
            }

            float[] resArray = new float[expectedCount];
            for (int i = 0; i < expectedCount; ++i)
            {
                if (!float.TryParse(parts[i], out resArray[i]))
                {
                    return null;
                }

                if (resArray[i] < min)
                {
                    return null;
                }

                if (resArray[i] > max)
                {
                    return null;
                }
            }

            return resArray;
        }

        /// <summary>
        /// Parse a byte array from the passed property name.
        /// </summary>
        /// <param name="name">Property name to parse a byte array from.</param>
        /// <param name="expectedCount">Expected number of items in the array.</param>
        /// <param name="min">Min value.</param>
        /// <param name="max">Max value.</param>
        /// <returns>Returns the byte array if we could parse it or null if we could not.</returns>
        protected byte[] ParseByteArray(string name, int expectedCount, byte min = byte.MinValue, byte max = byte.MaxValue)
        {
            string source = this.Properties.GetString(name, null);
            if (string.IsNullOrEmpty(source))
            {
                return null;
            }

            string[] parts = source.Split(' ');
            if (parts == null)
            {
                return null;
            }

            if (parts.Length != expectedCount)
            {
                return null;
            }

            byte[] resArray = new byte[expectedCount];
            for (int i = 0; i < expectedCount; ++i)
            {
                if (!byte.TryParse(parts[i], out resArray[i]))
                {
                    return null;
                }

                if (resArray[i] < min)
                {
                    return null;
                }

                if (resArray[i] > max)
                {
                    return null;
                }
            }

            return resArray;
        }

        #endregion
    }
}
